package com.csproj.dispatch.resource;

import com.codahale.metrics.annotation.Timed;
import com.csproj.basic.service.dto.WarehouseDTO;
import com.csproj.dispatch.service.WarnInfoService;
import com.csproj.dispatch.service.dto.WarnInfoDTO;
import com.csproj.dispatch.util.MyOzSmsClient;
import com.csproj.siiat.base.common.dto.BaseDTO;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * REST controller for managing WarnInfo.
 * 警告信息controller
 */
@RestController
@RequestMapping("/api/center/warn")
public class WarnInfoResource {

    private final Logger log = LoggerFactory.getLogger(WarnInfoResource.class);

    private static final String ENTITY_NAME = "warnInfo";

    private final WarnInfoService warnInfoService;

    public WarnInfoResource(WarnInfoService warnInfoService) {
        this.warnInfoService = warnInfoService;
    }

    /**
     * 插入警告信息
     * @param warnInfoDTO
     * @return
     */
    @PostMapping("/warninfo")
    @Timed
    public BaseDTO insertWarnInfo(@RequestBody WarnInfoDTO warnInfoDTO){
        return warnInfoService.insertWarnInfo(warnInfoDTO);
    }

    /**
     * 发送短信-更改通知状态
     * @param warnInfoDTO noticestatus 通知状态 0：未通知 1：通知成功 2：通知未成功
     * @param id 报警信息id
     * @return
     */
    @PutMapping("/warninfo/updatenstatus/{id}")
    @Timed
    public BaseDTO updateWarnInfoNotices(@RequestBody WarnInfoDTO warnInfoDTO, @PathVariable String id){
        return warnInfoService.updateWarnInfoNotices(id,warnInfoDTO);
    }

    /**
     * 报警信息设置已处理
     * @param warnInfoDTO handlereport 报警信息处理报告信息等
     * @param id 报警信息id
     * @return
     */
    @PutMapping("/warninfo/handle/{id}")
    @Timed
    public BaseDTO updateWarnInfoHandle(@RequestBody WarnInfoDTO warnInfoDTO, @PathVariable String id){
        return warnInfoService.updateWarnInfoHandle(id, warnInfoDTO);
    }

    /**
     * 获取此次调度的所有报警信息
     * @param dispatchId 调度信息id
     * @return
     */
    @GetMapping("/warninfo/dispatch/{dispatchId}")
    @Timed
    public BaseDTO searchWarnInfosOfDispatch(@PathVariable String dispatchId){
        return warnInfoService.searchWarnInfosOfDispatch(dispatchId);
    }

    /**
     * 获取报警信息-分页
     * @param page 页码
     * @param limit 单页条数
     * @param params 过滤参数
     * @return
     */
    @GetMapping("/warninfo/page")
    @Timed
    public BaseDTO searchAllWarnInfoPage(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit
            , @RequestParam(value = "params", required = false) String params){
        ObjectMapper mapper = new ObjectMapper();
        Map map = null;
        if(params!=null && !"".equals(params)){
            try {
                map=mapper.readValue(params, Map.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return warnInfoService.searchAllWarnInfoPage(page, limit, map);
    }

    @GetMapping("/warninfo/get/{id}")
    @Timed
    public  BaseDTO getWarnInfoById( @PathVariable String id)
    {
        WarnInfoDTO dto = warnInfoService.findOne(id);
        return BaseDTO.createBaseDTO(dto);
    }

    /**
     * 获取报警信息-分页
     * @param warnId 报警信息
     * @return
     */
    @GetMapping("/warninfo/sendmsg")
    @Timed
    public BaseDTO sendMessage(@RequestParam("warnId") String warnId,@RequestParam("phone") String phone,
                               @RequestParam("msg") String msg){
        return  warnInfoService.sendMessage(warnId,phone,msg);
    }


}